package com.leyou.order.controller;

import com.leyou.order.dto.CartToOrderDTO;
import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param cartToOrderDTO
     * @return
     */
    @PostMapping("/order")
    public ResponseEntity<Long> saveOrder(@RequestBody CartToOrderDTO cartToOrderDTO){
        Long orderId = orderService.saveOrder(cartToOrderDTO);
        return ResponseEntity.ok(orderId);
    }

    /**
     * 查询订单
     * @param id
     * @return
     */

    @GetMapping("/order/{id}")
    public ResponseEntity<OrderDTO> findOrderById(@PathVariable("id") Long id){
        OrderDTO orderDTO = orderService.findOrderById(id);
        return ResponseEntity.ok(orderDTO);
    }

    /**
     *微信支付生成二维码
     * @param id
     * @return
     */
    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long id){
        String payUrl = orderService.getPayUrl(id);
        return ResponseEntity.ok(payUrl);
    }

    /**
     * 微信支付订单成功更新状态
     * @param id
     * @return
     */
    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> getPayCode(@PathVariable("id") Long id){
        Integer payCode = orderService.getPayCode(id);
        return ResponseEntity.ok(payCode);
    }


}
